﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using static System.Math;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Forms;

namespace Random2
{
    public partial class Form1 : MetroForm
    {
        public Form1()
        {
            InitializeComponent();
        }

/* ===== INTEGER ===== */

        int intMin = 1;
        int intMax = 54;
        int intValue = 0;
        Random intRan = new Random();

        private void intMinNum_ValueChanged(object sender, EventArgs e)
        {
            intMin = (int)intMinNum.Value;
        }
        private void intMaxNum_ValueChanged(object sender, EventArgs e)
        {
            intMax = (int)intMaxNum.Value;
        }
        private void intRanBtn_Click(object sender, EventArgs e)
        {
            if (intMin <= intMax)
            {
                intValue = intRan.Next(intMin, intMax + 1);
                intValTxt.Text = intValue.ToString();
            }
            else
            {
                MessageBox.Show("\"最小值\"不能大于\"最大值\"。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

/* ===== PURE DECIMAL ===== */

        decimal pdTemp = 0;
        decimal pdValue = 0.00000M;
        Random pdRan = new Random();

        private void pdRanBtn_Click(object sender, EventArgs e)
        {
            pdTemp = pdRan.Next(0, 100000);
            pdValue = pdTemp / 100000;
            pdValTxt.Text = pdValue.ToString();
        }

/* ===== MIXED DECIMAL ===== */

        decimal mdMin = 0.00000M;
        int mdMinTemp = 0;

        decimal mdMax = 50.00000M;
        int mdMaxTemp = 5000000;

        decimal mdValueTemp = 0;
        decimal mdValue = 0.00000M;

        Random mdRan = new Random();

        private void mdMinNum_ValueChanged(object sender, EventArgs e)
        {
            mdMin = mdMinNum.Value;
            mdMinTemp = (int)(mdMin * 100000);
        }
        private void mdMaxNum_ValueChanged(object sender, EventArgs e)
        {
            mdMax = mdMaxNum.Value;
            mdMaxTemp = (int)(mdMax * 100000);
        }
        private void mdRanBtn_Click(object sender, EventArgs e)
        {
            if (mdMin <= mdMax)
            {
                mdValueTemp = mdRan.Next(mdMinTemp, mdMaxTemp + 1);
                mdValue = mdValueTemp / 100000;
                mdValTxt.Text = mdValue.ToString();
            }
            else
            {
                MessageBox.Show("\"最小值\"不能大于\"最大值\"。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

/* ===== ADVANCE ===== */

        int adMin = 1;
        int adMax = 54;
        byte[] adValue = new byte[16];
        int adValue2 = 0;
        RandomNumberGenerator adRan = RandomNumberGenerator.Create();

        private void adMinNum_ValueChanged(object sender, EventArgs e)
        {
            adMin = (int)adMinNum.Value;
        }
        private void adMaxNum_ValueChanged(object sender, EventArgs e)
        {
            adMax = (int)adMaxNum.Value;
        }
        private void adRanBtn_Click(object sender, EventArgs e)
        {
            if (adMin <= adMax)
            {
                adRan.GetBytes(adValue);
                adValue2 = BitConverter.ToUInt16(adValue, 0);
                if (adDecimalCkB.Checked)
                {
                    // Decimal...
                    adValTxt.Text = (decimal)adValue2 / UInt16.MaxValue * (adMax - adMin) + adMin + 1.ToString();
                }
                else
                {
                    // Integer...
                    adValTxt.Text = ((int)((decimal)adValue2 / UInt16.MaxValue * (adMax - adMin + 1) + adMin)).ToString();
                }
            }
            else
            {
                MessageBox.Show("\"最小值\"不能大于\"最大值\"。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

/* ===== ABOUT ===== */

        private void HomePageLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("https://gitee.com/l_mc/random2");
        }
    }
}
